package com.zbcz.erp.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.zbcz.erp.bean.BusinessAddBean
import com.zbcz.erp.network.ErpApi
import com.zbcz.erp.network.RetrofitManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

class ApprovalAdviceViewModel : ViewModel() {

    private val mApi = RetrofitManager.getRetrofit()?.create(ErpApi::class.java)
    var mActPassLiveData: MutableLiveData<Pair<Boolean, String?>> = MutableLiveData()
    var mActBackLiveData: MutableLiveData<Pair<Boolean, String?>> = MutableLiveData()

    fun actPass(
        isPass: Boolean,
        id: String,
        procInstId: String,
        comment: String?
    ) {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
//                val result = if (isPass) actPassSuspend(
//                    id,
//                    procInstId,
//                    comment
//                ) else actBackSuspend(id, procInstId, comment)
//                mActPassLiveData.postValue(result)
                if (isPass) {
                    mActPassLiveData.postValue(
                        actPassSuspend(
                            id,
                            procInstId,
                            comment
                        )
                    )
                } else {
                    mActPassLiveData.postValue(
                        actBackSuspend(
                            id,
                            procInstId,
                            comment
                        )
                    )
                }
            }
        }
    }


    private suspend fun actPassSuspend(
        id: String,
        procInstId: String,
        comment: String?
    ): Pair<Boolean, String?> {
        try {
            val result = mApi?.actTaskPass(id, procInstId, comment ?: "")
            return if (result?.code == 200 && result.success) {
                Pair(true, result.message)
            } else {
                Pair(false, result?.message)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return return Pair(false, "")
    }

    private suspend fun actBackSuspend(
        id: String,
        procInstId: String,
        comment: String?
    ): Pair<Boolean, String?> {
        try {
            val result = mApi?.actTaskBack(id, procInstId, comment ?: "")
            return if (result?.code == 200 && result.success) {
                Pair(true, result.message)
            } else {
                Pair(false, result?.message)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return return Pair(false, "")
    }

    private suspend fun uploadImageSuspend(file: File): String? {
        val requestBody = RequestBody.create("*/*".toMediaTypeOrNull(), file)
        val filePart = MultipartBody.Part.createFormData("file", file.name, requestBody)
        val result = mApi?.uploadImage(filePart)
        if (result?.success == true) {
            return result.message
        }
        return null
    }

}